
XfReturnDriverToSpawn = {
	private ["_ai","_vec_pos","_i"];
	_ai = _this select 0;
	_vec_pos = _this select 1;
	diag_log Format["Returning driver to spawn %1(%2), %3", _ai, typeof _ai, _vec_pos];
	_i = 0;
	(vehicle _ai) lockdriver false;
	_ai setCaptive False;
	_ai_hut = d_pos_ai_hut select 0;
	While {(vehicle _ai != _ai) And (alive _ai) And (alive (vehicle _ai)) And (_i < 60)} do {
		//TODO: Get out of the vehicle
		// 1) Check that the vehicle is landed, otherwise send it to the landing location.
		If ((getPos (vehicle _ai) select 2) > 10) then {
			_ai doMove _vec_pos;
			If (getPos (vehicle _ai) == _vec_pos) then {
				[vehicle _ai, _ai, _vec_pos] call XfChopperLanding;
			};
			sleep 1;
			diag_log Format["Moving vehicle to end pos"];
		} else {
			if (isEngineOn (vehicle _ai)) then {
				_ai action ["ENGINEOFF", vehicle _ai];
				sleep 4;
			};
			_ai doMove _ai_hut;
			// Move the AI out of the vehicle.
			_ai action ["eject",vehicle _ai];
			sleep 2;
			diag_log Format["Got out"];
		};
		_i = _i + 1;
	};
	if ((_i >= 60) or (vehicle _ai != _ai)) then {
		if (vehicle _ai != _ai) then {
			diag_log Format["Forcing get out %1 (%2)",typeof _ai, vehicle _ai];
			_ai doMove _ai_hut;
			(vehicle _ai) setVelocity [0,0,0];
			(vehicle _ai) setPos _vec_pos;
			sleep 0.1;
			_ai action ["ENGINEOFF", vehicle _ai];
			sleep 3;
			_ai action ["eject",vehicle _ai];
			sleep 5;
			_ai setPos [_vec_pos select 0 + 2, _vec_pos select 1 + 2, 0];
			sleep 2;
		};
		
	};
	_i = 0;
	if (((_ai_hut select 0 != 0) Or (_ai_hut select 1 != 0)) and (vehicle _ai == _ai)) then {
		While {(_ai distance _ai_hut > 5) And (alive _ai) And _i < 100} do {
			_ai doMove _ai_hut;
			sleep 1.1;
			_i = _i + 1;
		};	
	};
	diag_log Format["Pilot deleted %1 (%2)",typeof _ai, _ai];
	deleteVehicle _ai;
};

XfChopperLanding = {
	Private ["_heli","_pilot","_endpos"];
	_heli = _this select 0;
	_pilot = _this select 1;
	_endpos = _this select 2;
	while {(isEngineOn _heli) && (alive _pilot) && (damage _heli < 0.5)} do {
		if ((getPos _heli select 2) > ((_endpos select 2) + 1)) then {
			_heli setDir _dir;
			if ((getPos _heli select 2) > ((_endpos select 2) + 5)) then {
				_heli setVelocity [0,0,-7];
			} else {
				_heli setVelocity [0,0,7*((getPos _heli select 2)-(_endpos select 2))/-5 min -1];
			};
		} else {
			_pilot action ["ENGINEOFF", _heli];
			_heli setVelocity [0,0,0];
		};
		sleep 0.01;
	};

	for "_i" from 0 to 199 do {
		_heli setVelocity [0,0,-0.3];
		sleep 0.05;
	};
};

XfFindLandingPosition = {
	private ["_start_pos","_radius","_current_spot","_b_found_spot","_i_tries","_return_spot","_not_valid"];
	_start_pos = _this select 0;
	_vehicle_type = _this select 1;
	_radius = 200;
	diag_log Format["**Find landing pos: %1, %2, %3", _start_pos, _Vehicle_type, _radius];
	If (count _this > 2) then {	_radius = _this select 2; };
	
	_current_spot = _start_pos;
	_b_found_spot = False;
	_i_tries = 0;
	_return_spot = [];
	
	while { !_b_found_spot && (_i_tries < 100) } do {
		_i_tries = _i_tries + 1;
		_not_valid = false;
		diag_log Format["*  Trying(%1): %2",_i_tries, _current_spot];
		_current_spot = _current_spot findEmptyPosition [20,_radius,_vehicle_type];
		diag_log Format["*     |_ Nearest: %1", _current_spot];
		if (count _current_spot > 2) then {
			if (!(surfaceIsWater _current_spot)) then {
				_helper1 = "HeliHEmpty" createVehicleLocal [_current_spot select 0, (_current_spot select 1) + 4, 0];
				_helper2 = "HeliHEmpty" createVehicleLocal [_current_spot select 0, (_current_spot select 1) - 4, 0];
				_helper3 = "HeliHEmpty" createVehicleLocal [(_current_spot select 0) + 4, _current_spot select 1, 0];
				_helper4 = "HeliHEmpty" createVehicleLocal [(_current_spot select 0) - 4, _current_spot select 1, 0];
				if ((abs (((getPosASL _helper1) select 2) - ((getPosASL _helper2) select 2)) > 3) || (abs (((getPosASL _helper3) select 2) - ((getPosASL _helper4) select 2)) > 3)) then {
					diag_log Format["*     |_ Not valid: Slope too big"];
					_not_valid = true;
				} else {
					diag_log Format["*     |_ Valid: Valid spot: %1", _current_spot];
					_return_spot = _current_spot;
					_b_found_spot = True;
				};
				for "_mt" from 1 to 4 do {call compile format ["deleteVehicle _helper%1;", _mt]};
			} else {
				diag_log Format["*     |_ Not valid: Surface is water"];
				_not_valid = true;
			};		
		} else {
			diag_log Format["**  Not valid: Current pos not a position: %1", _current_spot];
			_not_valid = true;
		};
		if (_not_valid) then {
			_current_spot = [_start_pos, _radius] call XfGetRanPointCircle;
			diag_log Format["**  Generating a new pos: %1", _current_spot];
		};
	};
	
	_return_spot;
};
